package vaadin.lab6;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Field;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.PopupView;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class Lab extends Application {

    @Override
    public void init() {
        Window mainWindow = new Window("Lab", new FormLayout());
        mainWindow.getContent().setSizeUndefined();
        setMainWindow(mainWindow);
        final Item item = createItem();

        for (Object id : item.getItemPropertyIds()) {
            Property property = item.getItemProperty(id);
            final Field field = createFieldByProperty(id, property);
            mainWindow.addComponent(field);
        }

        PopupView popup = new PopupView(new PopupView.Content() {

            @Override
            public Component getPopupComponent() {
                final Table table = new Table();
                table.addContainerProperty("id", String.class, null);
                table.addContainerProperty("value", String.class, null);
                int i = 0;
                for (Object propertyId : item.getItemPropertyIds()) {
                    table.addItem(new Object[] { propertyId.toString(),
                            item.getItemProperty(propertyId).getValue() }, i++);
                }
                table.setPageLength(5);

                return table;
            }

            @Override
            public String getMinimizedValueAsHTML() {
                return "Show Item";
            }
        });

        mainWindow.addComponent(popup);
    }

    private static Item createItem() {
        PropertysetItem item = new PropertysetItem();

        item.addItemProperty("name", new ObjectProperty<String>(""));
        item.addItemProperty("price", new ObjectProperty<Double>(0.0));
        item.addItemProperty("options", new ObjectProperty<Set<String>>(
                new HashSet<String>(Arrays.asList("First"))));
        item.addItemProperty("available", new ObjectProperty<Date>(Calendar
                .getInstance().getTime()));

        return item;
    }

    public static Field createFieldByProperty(Object propertyId,
            Property property) {
        AbstractField field = null;
        final String caption = propertyId.toString();
        if (property.getType() == String.class) {
            field = new TextField(caption, property);
        } else if (property.getType() == Double.class) {
            field = new TextField(caption, new CurrencyPropertyFormatter(
                    property));
            field.addValidator(new CurrencyValidator(
                    "{0} is not a valid number"));
        } else if (Set.class.isAssignableFrom(property.getType())) {
            field = new OptionGroup(caption, Arrays.asList("First", "Second",
                    "Third"));
            ((OptionGroup) field).setMultiSelect(true);
            field.setPropertyDataSource(property);
        } else if (property.getType() == Date.class) {
            field = new PopupDateField(caption, property);
            ((PopupDateField) field).setResolution(DateField.RESOLUTION_DAY);
        }
        field.setImmediate(true);
        return field;
    }
}
